package com.easy.project.config.serialize;

import cn.hutool.core.collection.CollectionUtil;
import com.easy.project.constant.BooleanEnum;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.Formatter;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

import static cn.hutool.core.date.DatePattern.*;

/**
 * jackson序列化配置
 *
 * @author wxz
 */
@Slf4j
@Configuration
public class JacksonSerializeConfig {

    /**
     * LocalDate序列化
     */
    public static class ToLocalDateSerializer extends JsonSerializer<LocalDate> {

        @Override
        public void serialize(LocalDate localDate, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (Objects.nonNull(localDate)) {
                jsonGenerator.writeString(localDate.format(DateTimeFormatter.ofPattern(NORM_DATE_PATTERN)));
            }
        }
    }

    /**
     * LocalDateTime序列化
     */
    public static class ToLocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {

        @Override
        public void serialize(LocalDateTime localDateTime, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (Objects.nonNull(localDateTime)) {
                jsonGenerator.writeString(localDateTime.format(DateTimeFormatter.ofPattern(NORM_DATETIME_MINUTE_PATTERN)));
            }
        }
    }

    /**
     * Boolean 序列化
     */
    public static class ToBooleanSerializer extends JsonSerializer<Boolean> {
        @Override
        public void serialize(Boolean aBoolean, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
            if (aBoolean){
                jsonGenerator.writeNumber(BooleanEnum.TRUE.getCode());
                return;
            }
            jsonGenerator.writeNumber(BooleanEnum.FALSE.getCode());
        }
    }

    /**
     * 字符串日期转 LocalDate 统一规范只支持yyyy-MM-dd格式
     */
    /*public static class LocalDateFormatter implements Formatter<LocalDate> {
        @Override
        public LocalDate parse(String text, Locale locale) {
            return LocalDate.parse(text,DateTimeFormatter.ofPattern(NORM_DATE_PATTERN));
        }

        @Override
        public String print(LocalDate object, Locale locale) {
            return null;
        }
    }

    *//**
     * 字符串日期转 LocalDateTime 统一规范只支持yyyy-MM-dd HH:mm和yyyy-MM-dd HH:mm:ss格式
     *//*
    public static class LocalDateTimeFormatter implements Formatter<LocalDateTime> {

        private final List<String> localDateTimePatternList = CollectionUtil.newArrayList(NORM_DATETIME_PATTERN, NORM_DATETIME_MINUTE_PATTERN);

        @Override
        public LocalDateTime parse(String text, Locale locale) {
            Iterator<String> localDateTimePatternIterator = localDateTimePatternList.iterator();
            while (localDateTimePatternIterator.hasNext()){
                String pattern = localDateTimePatternIterator.next();
                if (localDateTimePatternIterator.hasNext()){
                    try {
                        return LocalDateTime.parse(text,DateTimeFormatter.ofPattern(pattern));
                    }catch (Exception ignored){
                        log.debug("LocalDateTimeFormatter no matching：{},value：{}",pattern,text);
                    }
                }else {
                    // 最后一个没有解析出来直接报异常
                    return LocalDateTime.parse(text,DateTimeFormatter.ofPattern(pattern));
                }
            }
            return null;
        }

        @Override
        public String print(LocalDateTime object, Locale locale) {
            return null;
        }
    }*/
}
